home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
develop, the CD; issue 1
/
Apple_Develop_1989.bin
/
Offscreen
/
Sample.spotlight
/
Sample.p
< prev
next >
Wrap
Text File
|
1989-06-19
|
32KB
|
906 lines
{------------------------------------------------------------------------------
#
# Apple Macintosh Developer Technical Support
#
# MultiFinder-Aware Simple Sample Application
#
# Sample
#
# Sample.p - Pascal Source
#
# Copyright © 1989 Apple Computer, Inc.
# All rights reserved.
#
# Versions:
# 1.00 08/88
# 1.01 11/88
# 1.02 04/89 MPW 3.1
#
# Components:
# Sample.p April 1, 1989
# Sample.c April 1, 1989
# Sample.a April 1, 1989
# Sample.inc1.a April 1, 1989
# SampleMisc.a April 1, 1989
# Sample.r April 1, 1989
# Sample.h April 1, 1989
# [P]Sample.make April 1, 1989
# [C]Sample.make April 1, 1989
# [A]Sample.make April 1, 1989
#
# Sample is an example application that demonstrates how to
# initialize the commonly used toolbox managers, operate
# successfully under MultiFinder, handle desk accessories,
# and create, grow, and zoom windows.
#
# It does not by any means demonstrate all the techniques
# you need for a large application. In particular, Sample
# does not cover exception handling, multiple windows/documents,
# sophisticated memory management, printing, or undo. All of
# these are vital parts of a normal full-sized application.
#
# This application is an example of the form of a Macintosh
# application; it is NOT a template. It is NOT intended to be
# used as a foundation for the next world-class, best-selling,
# 600K application. A stick figure drawing of the human body may
# be a good example of the form for a painting, but that does not
# mean it should be used as the basis for the next Mona Lisa.
#
# We recommend that you review this program or TESample before
# beginning a new application.
#
------------------------------------------------------------------------------}
PROGRAM Sample;
{Segmentation strategy:
This program consists of three segments. Main contains most of the code,
including the MPW libraries, and the main program. Initialize contains
code that is only used once, during startup, and can be unloaded after the
program starts. %A5Init is automatically created by the Linker to initialize
globals for the MPW libraries and is unloaded right away.}
{SetPort strategy:
Toolbox routines do not change the current port. In spite of this, in this
program we use a strategy of calling SetPort whenever we want to draw or
make calls which depend on the current port. This makes us less vulnerable
to bugs in other software which might alter the current port (such as the
bug (feature?) in many desk accessories which change the port on OpenDeskAcc).
Hopefully, this also makes the routines from this program more self-contained,
since they don't depend on the current port setting.}
USES
MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, Traps;
CONST
{MPW 3.0 will include a Traps.p interface file that includes constants for trap numbers.
These constants are from that file.}
{1.02 - since using MPW 3.0 only, we include Traps.p, so we now have trap numbers.}
{1.01 - changed constants to begin with 'k' for consistency, except for resource IDs}
{SysEnvironsVersion is passed to SysEnvirons to tell it which version of the
SysEnvRec we understand.}
kSysEnvironsVersion = 1;
{OSEvent is the event number of the suspend/resume and mouse-moved events sent
by MultiFinder. Once we determine that an event is an osEvent, we look at the
high byte of the message sent to determine which kind it is. To differentiate
suspend and resume events we check the resumeMask bit.}
kOSEvent = app4Evt; {event used by MultiFinder}
kSuspendResumeMessage = 1; {high byte of suspend/resume event message}
kResumeMask = 1; {bit of message field for resume vs. suspend}
kNoEvents = 0; {no events mask}
{1.01 - kMinHeap - This is the minimum result from the following
equation:
ORD(GetApplLimit) - ORD(ApplicZone)
for the application to run. It will insure that enough memory will
be around for reasonable-sized scraps, FKEYs, etc. to exist with the
application, and still give the application some 'breathing room'.
To derive this number, we ran under a MultiFinder partition that was
our requested minimum size, as given in the 'SIZE' resource.}
kMinHeap = 21 * 1024;
{1.01 - kMinSpace - This is the minimum result from PurgeSpace, when called
at initialization time, for the application to run. This number acts
as a double-check to insure that there really is enough memory for the
application to run, including what has been taken up already by
pre-loaded resources, the scrap, code, and other sundry memory blocks.}
kMinSpace = 8 * 1024;
{kExtremeNeg and kExtremePos are used to set up wide open rectangles and regions.}
kExtremeNeg = -32768;
kExtremePos = 32767 - 1; {required for old region bug}
{The following constants are all resource IDs, corresponding to resources in Sample.r.}
rMenuBar = 128; {application's menu bar}
rAboutAlert = 128; {about alert}
rUserAlert = 129; {error user alert}
rWindow = 128; {application's window}
rStopRect = 128; {rectangle for Stop light}
rGoRect = 129; {rectangle for Go light}
{The following constants are used to identify menus and their items. The menu IDs
have an "m" prefix and the item numbers within each menu have an "i" prefix.}
mApple = 128; {Apple menu}
iAbout = 1;
mFile = 129; {File menu}
iNew = 1;
iClose = 4;
iQuit = 12;
mEdit = 130; {Edit menu}
iUndo = 1;
iCut = 3;
iCopy = 4;
iPaste = 5;
iClear = 6;
mLight = 131; {Light menu}
iStop = 1;
iGo = 2;
{1.01 - kDITop and kDILeft are used to locate the Disk Initialization dialogs.}
kDITop = $0050;
kDILeft = $0070;
VAR
{The "g" prefix is used to emphasize that a variable is global.}
{GMac is used to hold the result of a SysEnvirons call. This makes
it convenient for any routine to check the environment. It is
global information, anyway.}
gMac : SysEnvRec; {set up by Initialize}
{GHasWaitNextEvent is set at startup, and tells whether the WaitNextEvent
trap is available. If it is false, we know that we must call GetNextEvent.}
gHasWaitNextEvent : BOOLEAN; {set up by Initialize}
{GInBackground is maintained by our osEvent handling routines. Any part of
the program can check it to find out if it is currently in the background.}
gInBackground : BOOLEAN; {maintained by Initialize and DoEvent}
{The following globals are the state of the window. If we supported more than
one window, they would be attatched to each document, rather than globals.}
{GStopped tells whether the stop light is currently on stop or go.}
gStopped : BOOLEAN; {maintained by Initialize and SetLight}
{GStopRect and gGoRect are the rectangles of the two stop lights in the window.}
gStopRect : Rect; {set up by Initialize}
gGoRect : Rect; {set up by Initialize}
{$S Initialize}
FUNCTION TrapAvailable(tNumber: INTEGER; tType: TrapType): BOOLEAN;
{Check to see if a given trap is implemented. This is only used by the
Initialize routine in this program, so we put it in the Initialize segment.
The recommended approach to see if a trap is implemented is to see if
the address of the trap routine is the same as the address of the
Unimplemented trap.}
{1.02 - Needs to be called after call to SysEnvirons so that it can check
if a ToolTrap is out of range of a pre-MacII ROM.}
BEGIN
IF (tType = ToolTrap) &
(gMac.machineType > envMachUnknown) &
(gMac.machineType < envMacII) THEN BEGIN {it's a 512KE, Plus, or SE}
tNumber := BAND(tNumber, $03FF);
IF tNumber > $01FF THEN {which means the tool traps}
tNumber := _Unimplemented; {only go to $01FF}
END;
TrapAvailable := NGetTrapAddress(tNumber, tType) <>
GetTrapAddress(_Unimplemented);
END; {TrapAvailable}
{$S Main}
FUNCTION IsDAWindow(window: WindowPtr): BOOLEAN;
{Check if a window belongs to a desk accessory.}
BEGIN
IF window = NIL THEN
IsDAWindow := FALSE
ELSE {DA windows have negative windowKinds}
IsDAWindow := WindowPeek(window)^.windowKind < 0;
END; {IsDAWindow}
{$S Main}
FUNCTION IsAppWindow(window: WindowPtr): BOOLEAN;
{Check